<?php
namespace Home\Model;
use Think\Model;
class LoginModel extends Model {
    protected $_validate = array(
//        array('verify','require','验证码必须！'), //默认情况下用正则进行验证
        array('login','require','用户名必须！'),
        array('login','#^13[\d]{9}$|^14[5,7]{1}\d{8}$|^15[^4]{1}\d{8}$|^17[0,6,7,8]{1}\d{8}$|^18[\d]{9}$#','手机号码格式错误！',0,'regex',3),
//        array('login','','用户名称已经存在！',0,'unique',1),
        array('yzm','require','图形验证码必须！'),
        array('dxyzm','require','短信验证码必须！'),
        array('psw','require','密码必须！'),
        array('qrpsw','require','确认密码必须！'),
    );

    //登录模型
    function login(){
        $data = $this->create();
        if(empty($data)){
            return;
        }

        $dbData = $this->getByLogin($data['login']);

        if(empty($dbData)){
            $this->error = "用户名不存在";
            return;
        }

        if($data['psw']!=$dbData['psw']){
            $this->error = "密码错误";
            return;
        }

        $yzm = $this->yzm($_POST['yzm']);
        if(!$yzm){
            $this->error = "验证码错误";
            return;
        }

        return $dbData['id'];
    }

    //注册模型
    function reg(){
        $model = D('login');
        $data = $model->create();
        $login = $model->getByLogin($_POST['login']);
        if(!$data){
            return $model->getError();
        }
        $yzm = $model->yzm($_POST['yzm']);
        if(!$yzm){
            return "验证码错误";
        }
        if($data['psw']!=$_POST['qrpsw']){
            return "确认密码不正确";
        }
        if($login){
            return "用户名已经存在";
        }else{
            $model->add();
        }
//        dump($data);
    }

    //验证码检测
    function yzm($code){
        $verify = new \Think\Verify();
        return $verify->check($code);
    }
}